/*
 * 
 */
package carAgents;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
//import java.awt.List;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;

// TODO: Auto-generated Javadoc
/**
 * The Class OfferAgentGui.
 */
class OfferAgentGui extends JFrame {	
	
	/** The my agent. */
	private OfferAgent myAgent;

	/** The start point street. */
	private JTextField startPointCountry, startPointCity, startPointStreet;
	
	/** The price. */
	private JTextField goalPointCountry, goalPointCity, goalPointStreet, price;
	
	/** The data picker. */
	private JTextField dataPicker;
	
	/** The time spinner. */
	private JSpinner timeSpinner;
	
	/** The clients. */
	private JComboBox clients;
	
	/** The plus button. */
	private JButton plusButton;
	
	/** The minus button. */
	private JButton minusButton;
	
	/** The tmp list. */
	private List<String> tmpList;
	
	/** The strarray. */
	private String[] strarray;
	
	/** The add button. */
	private JButton addButton;
	
	/** The points. */
	private ArrayList<PointWithTime> points;
	
	/** The list. */
	private JComboBox list;
	
	/** The win. */
	private JFrame deleteOfferFrame, win;

	/** The given offers. */
	private JComboBox givenOffers;

	/**
	 * Instantiates a new offer agent gui.
	 *
	 * @param a the a
	 */
	OfferAgentGui(OfferAgent a) {
		super(a.getLocalName());

		myAgent = a;

		JPanel p = new JPanel();
		p.setLayout(new GridLayout(7, 2));
		//p.add(new JLabel());
		p.add(new JLabel());
		p.add(new JLabel("Start Point:"));
		//p.add(new JLabel("Goal Point:"));

		p.add(new JLabel("Country:"));
		startPointCountry = new JTextField(15);
		//goalPointCountry = new JTextField(15);
		p.add(startPointCountry);
		//p.add(goalPointCountry);
		p.add(new JLabel("City:"));
		startPointCity = new JTextField(15);
		//goalPointCity = new JTextField(15);
		p.add(startPointCity);
		//p.add(goalPointCity);
		p.add(new JLabel("Street:"));
		startPointStreet = new JTextField(15);
		//goalPointStreet = new JTextField(15);
		price =  new JTextField(15);
		dataPicker = new JTextField(15);
		p.add(startPointStreet);
		//p.add(goalPointStreet);

		JPanel q1= new JPanel();
		q1.setLayout(new GridLayout(1,2));
		p.add(new JLabel("Date & Time:"));
		p.add(q1);
		q1.add(dataPicker);
		JButton b = new JButton("Pick a Date");
		q1.add(b);
		points =  new ArrayList<PointWithTime>();
		timeSpinner = new JSpinner( new SpinnerDateModel() );
		JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "HH:mm");
		timeSpinner.setEditor(timeEditor);
		//timeSpinner.setValue(new Date()); // will only show the current time

		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(1, 2));
		p1.add(new JLabel("Price"));
		p1.add(price);
		p.add(p1);
		p.add(timeSpinner);
		JButton add = new JButton("Add point");

		p.add(add);
		list = new JComboBox();


		p.add(list);
		//p.add(new JLabel());
		//p.add(new JLabel());
		//p.add(new JLabel());
		//p.add(dataPicker);
		final JFrame f = new JFrame();
		f.getContentPane().add(p);
		f.pack();
		//f.setVisible(true);
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				dataPicker.setText(new DatePicker(f).setPickedDate());

			}
		});


		getContentPane().add(p, BorderLayout.CENTER);

		addButton = new JButton("Offer");
		addButton.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				try {
					addButton.setVisible(false);
					price.setEnabled(true);
					List<PointWithTime> tmpPoints = new ArrayList<PointWithTime>(points);

					myAgent.updateCatalogue(tmpPoints,Double.parseDouble(price.getText()));

					System.out.println("added");
					points.clear();
					list.removeAllItems();
				}
				catch (Exception e) {
					JOptionPane.showMessageDialog(OfferAgentGui.this, "Wrong values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
				}
			}
		} );
		p = new JPanel();
		p.add(addButton);

		//addButton.setEnabled(false);
		addButton.setVisible(false);
		JButton ratingButton = new JButton("Rating");
		p.add(ratingButton);
		JButton deleteOfferButton = new JButton("Delete offer");
		p.add(deleteOfferButton);

		deleteOfferButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				deleteOfferFrame = new JFrame("Delete offer");
				deleteOfferFrame.setVisible(true);
				deleteOfferFrame.setSize(400, 200);


				String[] listOfBlocked = new String[myAgent.getCatalogue().size()];
				for(int i = 0; i < myAgent.getCatalogue().size(); i++) {
					listOfBlocked[i] = myAgent.getCatalogue().get(i).toString2();
				}

				givenOffers = new JComboBox(listOfBlocked);

				JPanel panel = new JPanel();
				final JButton deleteButton = new JButton("Delete");
				panel.setLayout(new GridLayout(2,1));
				panel.add(givenOffers);
				panel.add(deleteButton);
				deleteOfferFrame.add(panel);

				if (listOfBlocked.length == 0) {
					givenOffers.setEnabled(false);
					deleteButton.setEnabled(false);
				}
				else {
					givenOffers.setEnabled(true);
					deleteButton.setEnabled(true);
				}

				deleteButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						myAgent.deleteOffer(givenOffers.getSelectedIndex());
						//givenOffers.remove(givenOffers.getSelectedIndex());
						givenOffers.removeAllItems();
						String[] listOfBlocked = new String[myAgent.getCatalogue().size()];
						for(int i = 0; i < myAgent.getCatalogue().size(); i++) {
							listOfBlocked[i] = myAgent.getCatalogue().get(i).toString2();
							givenOffers.addItem(listOfBlocked[i].toString());
						}
						if (listOfBlocked.length == 0) {
							givenOffers.setEnabled(false);
							deleteButton.setEnabled(false);
						}
						else {
							givenOffers.setEnabled(true);
							deleteButton.setEnabled(true);
						}
						//deleteButton.setEnabled(false);
						//givenOffers.setEnabled(false);
						/*String[] listOfBlocked = new String[myAgent.getCatalogue().size()];
						for(int i = 0; i < myAgent.getCatalogue().size(); i++) {
							listOfBlocked[i] = myAgent.getCatalogue().get(i).toString2();
							givenOffers.addItem(listOfBlocked[i].toString());
						}
						if (givenOffers.getComponentCount() != 0) {
							deleteButton.setEnabled(true);
						}
						else {
							deleteButton.setEnabled(false);
						}*/
						deleteOfferFrame.repaint();

					}
				});


			}
		});

		add.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (price.getText().toString().equals("") || 
						startPointCountry.getText().equals("") ||
						startPointCity.getText().equals("") || 
						startPointStreet.getText().equals("") || 
						dataPicker.getText().equals("")) {
					JOptionPane.showMessageDialog(f,"You must fill all fields");
				}
				else {

					if (points.size() == 1){
						addButton.setVisible(true);
					}
					else if (points.size() == 0) {
						price.setEnabled(false);
					}
					PlacePoint startP = new PlacePoint(startPointCountry.getText().trim(),
							startPointCity.getText().trim(),
							startPointStreet.getText().trim());
					Long startDateHour=Tools.changeToInt(dataPicker.getText().trim(), ((Date)timeSpinner.getValue()).getHours()*100+((Date)timeSpinner.getValue()).getMinutes());

					points.add(new PointWithTime(startP, startDateHour));
					list.addItem(new PointWithTime(startP, startDateHour).toString());

				}


			}
		});
		ratingButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				win = new JFrame("Rating system");
				win.setVisible(true);
				win.setSize(200, 150);
				System.out.println(myAgent.getLocalName());
				tmpList = Tools.usersToRating(myAgent.getLocalName());
				System.out.println(tmpList.size());
				strarray = (String[]) tmpList.toArray(new String[0]);
				clients = new JComboBox(strarray);
				JPanel panel = new JPanel();
				JPanel panel2 = new JPanel();
				plusButton = new JButton("+");
				minusButton = new JButton("-");

				panel.setLayout(new GridLayout(2,1));
				panel.add(clients);
				panel2.setLayout(new GridLayout(1,2));
				panel2.add(plusButton);
				panel2.add(minusButton);
				panel.add(panel2);
				win.add(panel);

				if (strarray.length == 0) {
					plusButton.setEnabled(false);
					minusButton.setEnabled(false);
					clients.setEnabled(false);
				}
				else {
					plusButton.setEnabled(true);
					minusButton.setEnabled(true);
					clients.setEnabled(true);
				}

				plusButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						Tools.addRatingForRequest(clients.getSelectedItem().toString(), 1);
						Tools.deleteFromWaitForGrade(myAgent.getLocalName(), 
								clients.getSelectedItem().toString());

						tmpList.remove(clients.getSelectedIndex());
						clients.removeAllItems();
						if (tmpList.size() == 0) {
							plusButton.setEnabled(false);
							minusButton.setEnabled(false);
							clients.setEnabled(false);
						}
						else {
							plusButton.setEnabled(true);
							minusButton.setEnabled(true);
							clients.setEnabled(true);
							for (int i = 0; i < tmpList.size(); i++) {
								clients.addItem(tmpList.get(i));
							}
						}

						//win.repaint();

					}
				});
				minusButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						Tools.addRatingForRequest(clients.getSelectedItem().toString(), -1);
						Tools.deleteFromWaitForGrade(myAgent.getLocalName(), 
								clients.getSelectedItem().toString());

						JFrame frame = new JFrame();
						int n = JOptionPane.showConfirmDialog(
								frame,
								"Do you want to block this user?",
								"Question about block user",
								JOptionPane.YES_NO_OPTION);
						if (n == JOptionPane.YES_OPTION) {
							Tools.addToBlockList(myAgent.getLocalName(), 
									clients.getSelectedItem().toString());
						}

						tmpList.remove(clients.getSelectedIndex());
						clients.removeAllItems();
						if (tmpList.size() == 0) {
							plusButton.setEnabled(false);
							minusButton.setEnabled(false);
							clients.setEnabled(false);
						}
						else {
							plusButton.setEnabled(true);
							minusButton.setEnabled(true);
							clients.setEnabled(true);
							for (int i = 0; i < tmpList.size(); i++) {
								clients.addItem(tmpList.get(i));
							}
						}

						//win.repaint();
					}
				});
			}
		});

		getContentPane().add(p, BorderLayout.SOUTH);

		addWindowListener(new	WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				myAgent.doDelete();
			}
		} );

		setResizable(false);
	}

	/**
	 * Display.
	 */
	public void display() {
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)screenSize.getWidth() / 2;
		int centerY = (int)screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		setVisible(true);
	}	
}
